<?php
include_once 'data.php';
include_once 'functions.php';

$home = $url;

include_once 'index.inc.php';
?>

<body style="margin:0;border:0;padding:0;width:100%;height:100%">
    <div class="ui-state-highlight ui-corner-top" style="float:left;margin:2px 4px">
    <a href="<?php print $home; ?>" style="display:block;">
    <span class="ui-icon ui-icon-home" style="float:left"></span>I, Librarian&nbsp;
    </a>
</div>
<div style="clear:both"></div>
        <?php

			database_connect($database_path, 'library');
			
			if (isset($_GET['delete']) && isset($_GET['file']) && isset($dbHandle)) {
			    $error = delete_record($dbHandle, $_GET['file']);
			    die ();
			}
			
			if (isset($_GET['file'])) {
			
			    $id_query = $dbHandle->quote($_GET['file']);
			    $result = $dbHandle->query("SELECT * FROM library WHERE id=$id_query LIMIT 1");
			    $paper = $result->fetch(PDO::FETCH_ASSOC);
			    $result = null;
			
			    $current_record = array_search($_GET['file'], $_SESSION['export_files']);
			    isset($_SESSION['export_files'][$current_record-1]) ? $prevrecord = $_SESSION['export_files'][$current_record-1] : $prevrecord = null;
			    isset($_SESSION['export_files'][$current_record+1]) ? $nextrecord = $_SESSION['export_files'][$current_record+1] : $nextrecord = null;
			}
			
			$dbHandle = null;
			?>
	<div class="display-content" id="file-item-<?php echo $paper['id']?>" style="margin:0px;padding:0px; height:100%">
	    
	    <?php
	    if (!empty($paper['id'])) {
	
	        print '<div id="file-panel" style="width:100%;height:48%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc;overflow:auto">';
	        include 'file_top.php';
	        print '</div>';
	
	        /*
	        print '<table class="top" cellspacing=0 style="width:100%"><tr><td style="width:20em">';
	        
	        print '<div class="ui-state-highlight" id="leftindex-handle1" style="float:left;margin-right:4px" title="Resize left panel">
			<span class="ui-icon ui-icon-triangle-2-e-w" ></span></div>';
		
	        if (isset($_SESSION['auth']) && is_file("library/$paper[file]")) {
	            print '<div id="reload" class="ui-state-highlight" style="float:left">
			     <span class="ui-icon ui-icon-refresh" style="float:left"></span>Reload&nbsp;
			    </div>';
	        }
	
	        print '&nbsp;</td><td style="text-align:center">';
	
	        print '<table cellspacing=0 style="margin:auto"><tr><td>';
	
	        print '<div class="ui-state-highlight'.(empty($prevrecord) ? ' ui-state-disabled' : ' prevrecord').'" id="prev-item-'.$prevrecord.'">
			   <span class="ui-icon ui-icon-triangle-1-n" style="float:left"></span>Prev&nbsp;
			  </div>';
	
	        print '</td><td style="padding:0px 8px">';
	
	        print '<div class="ui-state-highlight backbutton noprint" title="Back to list view">
			  <span class="ui-icon ui-icon-squaresmall-close" style="float:left"></span>Close&nbsp;
			</div>';
	
	        print '</td><td>';
	
	        print '<div class="nextrecord ui-state-highlight'.(empty($nextrecord) ? ' ui-state-disabled' : '').'" id="next-item-'.$nextrecord.'">
			  <span class="ui-icon ui-icon-triangle-1-s" style="float:left"></span>Next&nbsp;
	              </div>';
	
	        print '</td></tr></table>';
	
	        print '</td><td style="width:20em">';
	
	        if (isset($_SESSION['auth']) && is_file("library/$paper[file]")) {
	            print '<div class="ui-state-highlight" id="file-pgdown" style="width:7em;float:right;margin-left:4px">Scroll Down</div>';
	            print '<div class="ui-state-highlight noprint file-pgup" style="width:6em;float:right">Scroll Up</div>';
	        }
	
	        print '<div id="resizeframe" class="ui-state-highlight" style="float:right;padding-left:2px;margin-right:4px" title="Resize top panel"><span class="ui-icon ui-icon-triangle-2-n-s"></span></div>';
	
	        print '&nbsp;</td></tr></table>';
	*/
	        if (is_file("library/$paper[file]") && isset($_SESSION['auth'])) {
	
	       /*     if (!isset($_SESSION['pdfviewer']) || (isset($_SESSION['pdfviewer']) && $_SESSION['pdfviewer'] == 'external'))
			print PHP_EOL."<table id=\"pdf-table\" cellspacing=0 style=\"width:100%;height:100%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc\"><tr><td height='100%'>
				<iframe name=\"pdf\" class=\"pdf-file\" src=\"".htmlspecialchars("library/$paper[file]#pagemode=none&scrollbar=1&navpanes=0&toolbar=1&statusbar=0&page=1&view=FitH,0")."\"
					frameborder=\"0\" class=\"noprint\" style=\"width:100%;height:100%\"></iframe></td></tr></table>";
	
	            if (isset($_SESSION['pdfviewer']) && $_SESSION['pdfviewer'] == 'internal')
	            print PHP_EOL.'<table id="pdf-table" class="noprint" cellspacing=0 style="width:100%;height:100%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc"><tr><td>
				<iframe name="pdf" class="pdf-file" src="'.htmlspecialchars('viewpdf.php?file='.$paper['file']).'&navpanes=0"
					frameborder="0" style="width:100%;height:100%"></iframe></td></tr></table>';
					
	*/
	        	if (!isset($_SESSION['pdfviewer']) || (isset($_SESSION['pdfviewer']) && $_SESSION['pdfviewer'] == 'external'))
	        		print PHP_EOL . "<table id=\"pdf-table\" cellspacing=0 style=\"width:100%;height:100%;border-top:1px solid #c6c8cc;border-bottom:1px solid #c6c8cc\"><tr><td height='100%'>
	        		<iframe name=\"pdf\" class=\"pdf-file\" src=\"" . htmlspecialchars('downloadpdf.php?file=' . $paper['file'] . '#pagemode=none&scrollbar=1&navpanes=0&toolbar=1&statusbar=0&page=1&view=FitH,0') . "\"
	        		frameborder=\"0\" class=\"noprint\" style=\"width:100%;height:100%\"></iframe></td></tr></table>";
	        	
	        	if (isset($_SESSION['pdfviewer']) && $_SESSION['pdfviewer'] == 'internal')
	        		print PHP_EOL . '<table id="pdf-table" class="noprint" cellspacing=0 style="width:100%;height:100%"><tr><td height="100%">
	        		<iframe name="pdf" class="pdf-file" src="' . htmlspecialchars('viewpdf.php?file=' . $paper['file']) . '&navpanes=0"
	        		frameborder="0" style="width:100%;height:100%"></iframe></td></tr></table>';
	/*
	            print '<div class="ui-state-highlight noprint file-pgup" style="width:6em;float:right">Scroll Up</div>';
	
	            print '<div class="ui-state-highlight" id="leftindex-handle2" style="float:left" title="Resize left panel">
			<span class="ui-icon ui-icon-triangle-2-e-w" ></span></div>';
	
	            print '<div class="ui-state-highlight backbutton noprint" style="width:6em;margin-left:47%" title="Back to list view">
				   <span class="ui-icon ui-icon-squaresmall-close" style="float:left"></span>Close&nbsp;
			    </div>';
			    */
	
	        } elseif (isset($_SESSION['auth'])) {
	
	            print '<div style="margin-top:10%;margin-left:45%;color:#b6b8bc;font-size:28px;width:200px"><b>No PDF</b></div>';
	        }
	
	    } else {
	        print '<p><b>Record does not exist.</b>';
	    }
	
	    include_once 'coins.php';
	    ?>
	</div>
	<script type="text/javascript">
	<?php if (!empty($error)) print "$('#dialog-error').html('<p><span class=\"ui-state-error-text\"><span class=\"ui-icon ui-icon-alert\" style=\"float:left;margin:0px 7px 20px 0px\"></span></span>$error</p>').dialog('option','title','Error!').dialog('open');"; ?>
	</script>
	</td></tr></table>
	<script type="text/javascript">
filetop.init();
</script>
    </body>
    
</html>